package service.Impl;

import dao.mapper.BookMapper;
import dao.mapper.ClerkMapper;
import org.apache.ibatis.session.SqlSession;
import service.IBookService;
import utils.SqlSessionUtil;
import vo.Book;
import vo.OrderLine;
import vo.PageBean;

import java.sql.SQLException;
import java.util.List;

public class IBookServiceImpl implements IBookService {
    SqlSession sqlSession = new SqlSessionUtil().sqlSession;
    BookMapper bookMapper = sqlSession.getMapper(BookMapper.class);

//    @Override
//    public List<Book> getNew() throws SQLException {
//        List<Book> books=bookMapper.getNew();
//        return books;
//    }
//
//    @Override
//    public PageBean<Book> getByPage(Integer currPage) throws SQLException {
//        List<Book> books=bookMapper.getPage(currPage,25);
//        return changeintoPB(books,currPage);
//    }
//
//    @Override
//    public PageBean<Book> getByName(String name) throws SQLException {
//
//        List<Book> books=bookMapper.findByName(name);
//        return changeintoPB(books,0);
//    }
//
    @Override
    public void addBook(Book book){
        bookMapper.addBook(book);
        sqlSession.commit();
    }

    @Override
    public void deleteBook(Integer id){
        bookMapper.deleteBook(id);

    }
//
//    @Override
//    public void updateBook(Book book) throws SQLException {
//        bookMapper.updateBook(book);
//
//    }

    @Override
    public Book selectBookById(int bookId) {
        Book book=new Book();
        book=bookMapper.selectBook(bookId);
        return book;
    }

    @Override
    public List<Book> getAllBook() {
        List<Book> books = bookMapper.selectAllBook();
        return books;
    }

    @Override
    public void changeInventory(int orderId,String state) {
        IOrderLineServiceImpl orderLineService=new IOrderLineServiceImpl();
        List<OrderLine> orderLines=orderLineService.selectOrderLineByOrderId(orderId);
        for(OrderLine orderLine:orderLines){
            int bookId=orderLine.getBookId();
            int quanlity=orderLine.getQuanlity();
            Book book=bookMapper.selectBook(bookId);
            int oldInventory=book.getInventory();
            if(state.equals("add")){
                int inventory=oldInventory+quanlity;
                bookMapper.changeInventoryById(bookId,inventory);
            }else if(state.equals("cut")){
                if(oldInventory<quanlity){
                    return;
                }
                int inventory=oldInventory-quanlity;
                bookMapper.changeInventoryById(bookId,inventory);
            }

        }

        sqlSession.commit();
    }

//    private  PageBean<Book> changeintoPB (List<Book> books,int currPage){
//        PageBean<Book> pbooks = new PageBean<Book>();
//        pbooks.setList(books);
//        //not sure, if needed modified in pagebean
//        pbooks.setPageEach(25);
//        pbooks.setCurrPage(currPage);
//        pbooks.setTotalCount(books.size());
//        pbooks.setTotalPage(pbooks.getTotalCount()/pbooks.getPageEach());
//        return pbooks;
//    }
}
